<?php


namespace app\api\controller\user;

use app\api\model\user\BusinessMoneyRecord;
use app\common\controller\Api;
use think\Db;

/**
 * 商家管理
 * 一个用户可以入驻一个商家 还是 可以入驻多个商家？本模块默认一个用户只能入驻一个商家。
 * */
class Business extends Api
{
    protected $noNeedLogin = [];
    protected $model = null;
    protected $business = null;

    public function _initialize()
    {
        $this->model = new BusinessMoneyRecord();
        parent::_initialize(); // TODO: Change the autogenerated stub
        $business = Db::name('business')->where(['user_id' => $this->auth['id'],'status'=>'normal'])->field('id,name,turnover,sound')->find();
        if (!$business) {
            $this->error('抱歉！您的账号未具备浏查看该项的权限');
        }else{
            $this->business = $business;
        }
    }

    /**
     * @title 商家中心(GET)
     * @url api/user.business/center
     * */
    public function center()
    {
        $this->success('获取成功',$this->business);
    }

    /**
     * @title 商家收支明细记录(GET)
     * @url api/user.business/moneyRecord
     * @param integer page 页数
     * */
    public function moneyRecord()
    {
        $page = $this->request->get('page', 1, 'intval');
        $page = max(1, $page);
        $page = ($page - 1) * 15;
        $list = $this->model->where(['user_id' => $this->auth['id']])->limit($page, 15)->order('id', 'desc')->select();
        foreach ($list as $row) {
            $row->visible(['id', 'type', 'record_type', 'money', 'balance', 'createtime']);
            if ($row->type == '出账') {
                $row->money = '-' . $row->money;
            } elseif ($row->type == '入账') {
                $row->money = '+' . $row->money;
            }
        }
        $this->success('获取成功', $list);
    }

    /**
     * @title 商家收支明细详情(GET)
     * @url api/user.business/moneyRecordDetail
     * @param integer record_id 记录ID
     * */
    public function moneyRecordDetail()
    {
        $record_id = $this->request->get('record_id', 0, 'intval') ?: $this->error('请求参数不正确');
        $row = $this->model->where(['id' => $record_id, 'user_id' => $this->auth['id']])->find();
        if (!$row) {
            $this->error('记录不存在');
        }
        if ($row->type == '出账') {
            $row->money = '-' . $row->money;
        } elseif ($row->type == '入账') {
            $row->money = '+' . $row->money;
        }
        $row->hidden(['user_id', 'business_id']);
        $this->success('获取成功', $row);
    }

    /**
     * @title 商家提现记录(GET)
     * @url api/user.busiess/extractRecord
     * @param integer page 页数
     * */
    public function extractRecord()
    {
        $page = $this->request->get('page', 1, 'intval');
        $page = max(1, $page);
        $page = ($page - 1) * 15;
        $list = $this->model->where(['user_id' => $this->auth['id'], 'type' => 'expend', 'record_type' => 'extract'])->limit($page, 15)->order('id', 'desc')->select();
        foreach ($list as $row) {
            $row->visible(['id', 'type', 'record_type', 'money', 'balance', 'createtime']);
            $row->money = '-' . $row->money;

        }
        $this->success('获取成功', $list);
    }

    /**
     * @title 商家提现记录详情(GET)
     * @url api/user.business/extractRecordDetail
     * @param integer record_id 记录ID
     * */
    public function extractRecordDetail()
    {
        $record_id = $this->request->get('record_id', 0, 'intval') ?: $this->error('请求参数不正确');
        $row = $this->model->where(['id' => $record_id, 'user_id' => $this->auth['id'], 'type' => 'expend', 'record_type' => 'extract'])->find();
        if (!$row) {
            $this->error('记录不存在');
        }
        $row->money = '-' . $row->money;

        $row->hidden(['user_id', 'business_id']);
        $this->success('获取成功', $row);
    }

    /**
     * @title 切换收款铃声开关(GET)
     * @url api/user.business/setSound
     * @param integer status 开关状态
     * @param integer business_id 商家ID
     * */
    public function setSound()
    {
        $status = $this->request->get('stauts',0,'intval');
        $res = Db::name('business')->where(['id'=>$this->business['id'],'user_id'=>$this->auth['id']])->update(['sound'=>$status]);
        if ($res) {
            $this->success('设置成功');
        } else {
            $this->error('设置失败');
        }
    }

    /**
     * @title 获取商家收款二维码(GET)
     * @url api/user.business/getQrcode
     * */
    public function getQrcode()
    {
        $params = [
            'user_id' => $this->auth['id'],
            'business_id'=>$this->business['id']
        ];
        Vendor('phpqrcode.phpqrcode');
        $object = new \QRcode();
        $errorCorrectionLevel = 'L';    //容错级别
        $matrixPointSize = 5;            //生成图片大小
        //打开缓冲区
        ob_start();
        //生成二维码图片
        $returnData = $object->png(json_encode($params), false, $errorCorrectionLevel, $matrixPointSize, 2);
        //这里就是把生成的图片流从缓冲区保存到内存对象上，使用base64_encode变成编码字符串，通过json返回给页面。
        $imageString = base64_encode(ob_get_contents());
        //关闭缓冲区
        ob_end_clean();
        $base64 = "data:image/png;base64," . $imageString;

        $data = [];
        base64_image_content($base64, ROOT_PATH . '/public/uploads', 'business-' . $this->business['id'] . '-qrcode');
        $data['qrcode'] = config('config.domain') . 'uploads/' . date('Ymd', time()) . '/business-' . $this->business['id'] . '-qrcode.png';
        $this->success('二维码生成成功', $data);
    }
}